package com.qiwo.webshow.controller;


import java.util.*;

import com.qiwo.webshow.domain.GoverTypeDO;
import com.qiwo.webshow.service.GoverTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qiwo.common.base.AdminBaseController;
import com.qiwo.webshow.domain.GoverProcurementDO;
import com.qiwo.webshow.service.GoverProcurementService;
import com.qiwo.common.utils.Result;

import javax.swing.text.html.Option;

/**
 * 
 * <pre>
 * 政府采购管理
 *  @author  chenwenchao
 *  @date 2018-10-23 14:57:15
 * </pre>
 * <small> 2018-10-23 14:57:15 | chenwenchao</small>
 */
@Controller
@RequestMapping("/webshow/goverProcurement")
public class GoverProcurementController extends AdminBaseController {
	@Autowired
	private GoverProcurementService goverProcurementService;
	@Autowired
	private GoverTypeService goverTypeService;

	@GetMapping()
	@RequiresPermissions("webshow:goverProcurement:goverProcurement")
	String GoverProcurement(Model model){
		List<GoverTypeDO> goverTypeDOList=goverTypeService.selectList(null);
		model.addAttribute("catalogs",goverTypeDOList);
		return "webshow/goverProcurement/goverProcurement";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("webshow:goverProcurement:goverProcurement")
	public Result<Page<GoverProcurementDO>> list(GoverProcurementDO goverProcurementDTO){
		List<GoverTypeDO> goverTypeDOList=goverTypeService.selectList(null);
        Wrapper<GoverProcurementDO> wrapper = new EntityWrapper<GoverProcurementDO>().orderBy("type_code",false).orderBy("sort", false);

		if(goverProcurementDTO!=null){
			if(goverProcurementDTO.getTypeCode()!=null){
				wrapper.eq("type_code",goverProcurementDTO.getTypeCode());
			}
			if(goverProcurementDTO.getTitle()!=null){
				wrapper.like("title",goverProcurementDTO.getTitle());
			}
		}
		Page<GoverProcurementDO> page = goverProcurementService.selectPage(getPage(GoverProcurementDO.class), wrapper);

        for (GoverProcurementDO goverProcurementDO:page.getRecords()){
			Optional<GoverTypeDO> optionalGoverTypeDo=goverTypeDOList.stream().filter(f->{
				if(goverProcurementDO.getTypeCode()!=null) {
					return f.getId().toString().equals(goverProcurementDO.getTypeCode().toString());
				}else{
					return  false;
				}
			}).findFirst();
			if(optionalGoverTypeDo.isPresent()){
				goverProcurementDO.setTypeName(optionalGoverTypeDo.get().getName());
			}
		}


        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("webshow:goverProcurement:add")
	String add(Model model){
		List<GoverTypeDO> goverTypeDOList=goverTypeService.selectList(null);
		model.addAttribute("catalogs",goverTypeDOList);
	    return "webshow/goverProcurement/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("webshow:goverProcurement:edit")
	String edit(@PathVariable("id") Long id,Model model){
		GoverProcurementDO goverProcurement = goverProcurementService.selectById(id);
		model.addAttribute("goverProcurement", goverProcurement);
		List<GoverTypeDO> goverTypeDOList=goverTypeService.selectList(null);
		model.addAttribute("catalogs",goverTypeDOList);
	    return "webshow/goverProcurement/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("webshow:goverProcurement:add")
	public Result<String> save( GoverProcurementDO goverProcurement){
		goverProcurementService.insert(goverProcurement);
        return Result.ok();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("webshow:goverProcurement:edit")
	public Result<String>  update( GoverProcurementDO goverProcurement){
		boolean update = goverProcurementService.updateById(goverProcurement);
		return update ? Result.ok() : Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("webshow:goverProcurement:remove")
	public Result<String>  remove( Long id){
		goverProcurementService.deleteById(id);
        return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("webshow:goverProcurement:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		goverProcurementService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
